package com.cnap.multiclouldmgmt.util;

import com.cnap.multiclouldmgmt.model.ssh.SshConnection;
import org.apache.http.entity.ContentType;
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.channel.ChannelExec;
import org.apache.sshd.client.channel.ClientChannelEvent;
import org.apache.sshd.client.future.ConnectFuture;
import org.apache.sshd.client.future.DefaultAuthFuture;
import org.apache.sshd.client.future.DefaultConnectFuture;
import org.apache.sshd.client.session.ClientSession;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

class SshUtilsTest {

    @Test
    void runCommand() throws Exception {

        SshConnection conn = new SshConnection("username","password",CnapCloudMgmtConstants.TEST_ADMIN);
        SshClient sshClient = Mockito.mock(SshClient.class);
        ConnectFuture cf = Mockito.mock(ConnectFuture.class);
        Mockito.when(sshClient.connect("username",CnapCloudMgmtConstants.TEST_ADMIN,22)).thenReturn(cf);
        DefaultConnectFuture connectFuture = Mockito.mock(DefaultConnectFuture.class);
        Mockito.when(cf.verify()).thenReturn(connectFuture);
        ClientSession session = Mockito.mock(ClientSession.class);
        Mockito.when(connectFuture.getSession()).thenReturn(session);
        DefaultAuthFuture auth = Mockito.mock(DefaultAuthFuture.class);
        Mockito.when(session.auth()).thenReturn(auth);
        ChannelExec ce = Mockito.mock(ChannelExec.class);
        Mockito.when(session.createExecChannel("cmd")).thenReturn(ce);
        Set<ClientChannelEvent> events = new HashSet<>();
        Mockito.when(ce.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), TimeUnit.SECONDS.toMillis(20l)))
                        .thenReturn(events);
        Mockito.when(ce.getExitStatus())
                .thenReturn(0);
        SshUtils.setSshClient(sshClient);
        SshUtils.runCommand(conn,"cmd",20l);
        SshUtils.setSshClient(sshClient);
        SshUtils.runCommand(conn,"cmd");


    }

    @Test
    void runCommands() {
    }

    @Test
    void testRunCommands() {
    }

    @Test
    void testRunCommand() {
    }
}